package com.dl.helloworld;

import static android.view.View.GONE;

import android.animation.ValueAnimator;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.lifecycle.ViewModelProvider;

import com.dl.helloworld.ViewModel.DownLoadViewModel;
import com.dl.helloworld.myview.CircularProgressBar;

import java.io.IOException;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends BaseActivity {

    private EditText accountEdit;
    private EditText passwordEdit;

    private Button login;
    private Button sendHttp;
    private TextView respn;
    private TextView text;
    private Button changebtn;
    private int currentProgress = 0;
    DownLoadViewModel downLoadViewModel;
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 1:
                    text.setText("changed");
                    break;
                default:
                    break;
            }
        }
    };



    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        accountEdit = findViewById(R.id.account);
        passwordEdit = findViewById(R.id.password);
        login = findViewById(R.id.login);
        sendHttp = findViewById(R.id.sendhttp);
        respn = findViewById(R.id.response_text);
        text = findViewById(R.id.text);
        changebtn = findViewById(R.id.change_text);

        login.setOnClickListener(v -> {
            String account = accountEdit.getText().toString();
            String pwd = passwordEdit.getText().toString();

            if (account.equals("admin") && pwd.equals("123456")) {
                Intent intent = new Intent(LoginActivity.this, FirstActivity.class);
                startActivity(intent);
                finish();
            } else {
                Toast.makeText(LoginActivity.this, "密码错误", Toast.LENGTH_SHORT).show();
            }
        });

        sendHttp.setOnClickListener(v -> {
            Intent intent = new Intent(LoginActivity.this, ViewpagerActivity.class);
            startActivity(intent);
            finish();
            sendRequestWithOkhttp();
        });

        changebtn.setOnClickListener(v -> {
            new Thread(() -> {
                Message msg = new Message();
                msg.what = 1;
                handler.sendMessage(msg);
            }).start();
//             text.setText("changed");

        });

        // 在 Activity 中触发动画
        CircleView circleView = findViewById(R.id.circle_view);
        ValueAnimator animator = ValueAnimator.ofArgb(Color.RED, Color.BLUE);
        animator.setDuration(1000); // 动画时长 1 秒
        animator.addUpdateListener(a ->{
            int color = (int) a.getAnimatedValue();
            circleView.setCircleColor(color); // 需在 CircleView 中添加 setCircleColor 方法
        });
        animator.start();

        CircularProgressBar bar = findViewById(R.id.circularProgressBar);
        Button upbutton =findViewById(R.id.updateProgress);

        downLoadViewModel = new ViewModelProvider(this).get(DownLoadViewModel.class);

        downLoadViewModel.getProgressLiveData().observe(this,progress ->{
            bar.setProgress(progress.getProgress());
            if (progress.getProgress() == 100){
                bar.setProgress(0);
            }
        });

        upbutton.setOnClickListener(v->{
            downLoadViewModel.startDownload();
        });



    }

    private void sendRequestWithOkhttp() {
        new Thread(() -> {
            OkHttpClient client = new OkHttpClient();
            Request request = new Request.Builder().url("https://www.baidu.com").build();
            try {
                Response response = client.newCall(request).execute();
                if (response.isSuccessful() && response.body() != null) {
                    String responseData = response.body().string();
                    showResponse(responseData);
                }
            } catch (IOException e) {
                e.printStackTrace();
                // 显示错误信息给用户
                showErrorToast("网络请求出错：" + e.getMessage());
            }
        }).start();
    }

    private void showResponse(final String response) {
        runOnUiThread(() -> {
            respn.setText(response);
        });
    }

    private void showErrorToast(final String message) {
        runOnUiThread(() -> {
            Toast.makeText(LoginActivity.this, message, Toast.LENGTH_SHORT).show();
        });
    }
}